<?php
/**
 * @property Order $Order
 * @property EmailComponent $Email
 */
class OrdersController extends AppController {
    public $uses = array('Order','Product','User');
    public $name = 'Orders';
    public $helpers = array('Html', 'Form');
    public $paginate=array(
        'limit'=>'20',
        'order'=>array('Order.created'=>'desc')
    );
    public $components = array('Email');

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(array("add_to_cart","show_cart",'delete_product'));
        //debug($this->actionMap);
        $this->actionMap=array(
            "create"=>array("check_out"),
            "read"=>array("admin_user_orders")
        );


    }

    function _access($op,$params) {
        $op = $op[0];
        //debug($op);
        if(array_key_exists('admin', $params) && $params['admin'] == true) {
            return $this->Authorize->check('administer data');
        }
        switch ($op) {
            case 'read':
                if($this->params["action"]=="view") {
                    $this->Order->recursive = 0;
                    $order = $this->Order->read("user_id",$this->params["pass"][0]);
                    return $this->Auth->user("id")== $order["Order"]["user_id"];
                }
                return true;
                break;
            default : return false;
        }
    }

    function admin_index() {
        $this->set("orders",$this->paginate());
    }

    function admin_view($id=null) {
        if(!$id) {
            $this->Session->setFlash("Orders is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        $this->Order->recursive=1;
        $this->set('orders',$this->Order->read(null,$id));
    }

    function admin_delete($id=null) {
        if(!$id) {
            $this->Session->setFlash("Order is invalid");
            $this-redirect(array('action'=>'index'));
        }
        if (!$this->Order->del($id)) {
            $this->Session->setFlash("Order not exists!");
        }
        else {
            $this->Session->setFlash("Order deleted!");
        }
        $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
    }

    function admin_edit($id=null) {
        if(!$id && empty($this->data)) {
            $this->Session->setFlash("Order is invalid!");
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if($this->Order->save($this->data)) {
                $this->Session->setFlash("The saving is ready!");
                $this->redirect(array('action'=>'index'));
            }
            else {
                $this->Session->setFlash("The saving not ready, plase check the data");
            }
        }
        else {
            $this->data = $this->Order->read(null,$id);
        }
    }

    function view ($id=null) {
        if(!$id) {
            $this->Session->setFlash("Orders is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        }
        $this->Order->recursive=1;
        $this->set('orders',$this->Order->read(null,$id));
    }

    function index() {
        $orders=$this->paginate('Order',array('user_id'=>$this->Auth->user("id")));
        $this->set(compact("orders"));
    }

    function show_cart() {
        /*
         * Nem kell törzs, mert a Cart komponens átadja automatikusan: $cart
         */
    }

    function delete_product($product_id, $size_id) {
        $this->Cart->delete($product_id, $size_id);
        $this->redirect(array("action"=>"show_cart"));

    }

    function check_out() {
        $order = $this->Cart->checkOut();
        if (!$order){
            $this->Session->setFlash("Order not saved!");
        } else {
            $this->_sendOrderMail($order);
//            $this->_sendOrderAdminMail($order);
            $this->Session->setFlash("Order saved!");
            $this->Cart->deleteCart();
        }

        $this->redirect(array('controller'=>'orders','action'=>'index'));
    }

    function admin_user_orders($id=null) {
        if(!$id) {
            $this->Session->setFlash("Orders is invalid!");
            $this->redirect(array('action'=>'index'));//ezzel ugrik vissza az előző oldalra
        };
        $this->Order->recursive=-1;
        $this->Order->User->recursive=-1;
        $this->set("user",$this->Order->User->read("",$id));
        $this->set("Orders",$this->paginate('Order',array('user_id'=>$id)));
    }

    function _sendOrderMail($data){
        $this->_setSMTP();
        Configure::load('ordermail');
        $user = $this->User->read('', $this->Auth->user('id'));
        $this->Email->to = $user['User']['email'];
        $this->Email->subject = Configure::read('Order.Mail.subject');
        $this->Email->replyTo = Configure::read('Order.Mail.replyTo');
        $this->Email->from    = Configure::read('Order.Mail.from');
        $this->Email->template = 'order_mail_client';
        $this->Email->sendAs = 'html';
        $this->set('orderData', $data);
        $this->set('user', $user);

        $this->Email->send();
    }

    function _sendOrderAdminMail($data){
        $this->_setSMTP();
        Configure::load('ordermail');
        $user = $this->User->read('', $this->Auth->user('id'));
        
        $admins = Configure::read('Order.Mail.admin');
        if(!is_array($admins)){
            $admins = array($admins);
        }
        $this->Email->subject = Configure::read('Order.Mail.adminsubject');
        $this->Email->replyTo = $user['User']['email'];
        $this->Email->from    = $user['User']['name'];
        $this->Email->template = 'order_mail_admin';
        $this->Email->sendAs = 'html';
        $this->set('orderData', $data);
        $this->set('user', $user);
        
        foreach ($admins as $to){
            $this->Email->to = $to;
            $this->Email->send();
        }
    }
    
    function _setSMTP(){
        $this->Email->smtpOptions = array(
            'host' => 'mail.vezer.elte.hu',
            'username'=>'rychee@vezer.elte.hu',
            'password'=>'ricsi87'
        );
        $this->Email->delivery = 'smtp';
    }
}
?>